package com.example.spring_mybatis.mapper;

import com.example.spring_mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025/6/17
 * Time: 18:09
 */
@Mapper
public interface UserInfoMapper {

    @Insert("insert into user_info (username,password,age,gender) values (#{username},#{password},#{age},#{gender})")
    Integer getUserInfoInsertDemo1(UserInfo userInfo);

    @Insert("insert into user_info (username,password,age,gender) values (#{userInfo.username},#{userInfo.password}," +
            "#{userInfo.age},#{userInfo.gender})")
    Integer getUserInfoInsertDemo2(@Param("userInfo") UserInfo userInfo);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info (username,password,age,gender) values (#{username},#{password},#{age},#{gender})")
    Integer getUserInfoInsertDemo3(UserInfo userInfo);

    @Delete("delete from user_info where id = #{id}")
    Integer getUserInfoDeleteDemo1(Integer id);

    @Update("update user_info set gender = 1 where gender = #{gender}")
    Integer getUserInfoUpdateDemo1(Integer gender);
    //resultMap
    @ResultMap("resultMap")
    @Select("select * from user_info")
    List<UserInfo> getUserInfoAll1();
    //起别名
    @Select("select id,username,password,age,gender,phone," +
            "delete_flag as deleteFlag,create_time as createTime," +
            "update_time as updateTime from user_info")
    List<UserInfo> getUserInfoAll2();
    //指定映射关系
    @Results(id = "resultMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select * from user_info")
    List<UserInfo> getUserInfoAll3();

    @Select("select * from user_info")
    List<UserInfo> getUserInfoAll4();

    @Select("select * from user_info where delete_flag = #{deleteFlag}")
    List<UserInfo> getUserInfoSelectDemo1(Integer deleteFlag);

    @Select("select * from user_info where delete_flag = #{deleteFlag} and gender = #{gender}")
    List<UserInfo> getUserInfoSelectDemo2(Integer deleteFlag,Integer gender);

    @Select("select * from user_info where delete_flag = #{deleteFlag} and gender = #{gender}")
    List<UserInfo> getUserInfoSelectDemo3(UserInfo userInfo);
    //注入攻击
    @Select("select * from user_info where username = '${userName}'")
    List<UserInfo> getUserInfoSelectDemo4(String userName);
    //登录
    @Select("select * from user_info where username = '${userName}' and password = '${password}'")
    List<UserInfo> loginByUserNameOrPassword(String userName, String password);
    //排序
    @Select("select * from user_info order by id ${order}")
    List<UserInfo> getUserInfoSelectOrderById(String order);
    //模糊查询
    @Select("select * from user_info where username like concat('%',#{userName},'%')")
    List<UserInfo> getUserInfoSelectByLike(String userName);
}
